package dao.impl;


import dao.GoodsDao;
import model.Goods;
import utils.DaoUtil;
import utils.GenicaDaoImpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class GoodsDaoImpl extends GenicaDaoImpl<Goods,Integer> implements GoodsDao {


    @Override
    public Integer findLastGoods() {
        Object[] pramas = new Object[]{1};
        String sql = "SELECT * FROM goods ORDER BY id DESC LIMIT ? ";
         Goods goods  = DaoUtil.query(Goods.class,sql,pramas);
        System.out.println(goods.getId());
        return goods.getId();
    }

    @Override
    public List<Goods> searchByName(String keyword) {
        List<Goods> goods = null;
        Object[] pramas = new Object[]{keyword};
        String sql ="SELECT * FROM goods WHERE name LIKE ?";
        goods = DaoUtil.queryList(Goods.class,sql,pramas);
        return goods;
    }

    @Override
    public List<Goods> search(String keyword, Integer category, Integer state) {
        List<Goods> goods = null;
        Object[] pramas = null;
        StringBuffer sql = new StringBuffer("SELECT * FROM goods WHERE category_id= ? AND state = ?");
        if (!(keyword.equals("%%"))){
            sql.append(" AND name LIKE ? ;");
            pramas = new Object[]{category,state,keyword};
        }else {
            pramas = new Object[]{category, state};
        }
        goods = DaoUtil.queryList(Goods.class,sql.toString(),pramas);
        return goods;
    }

    @Override
    public List<String> findColor(String name) {
        List<String> colors = new ArrayList<>();
        String sql = "select distinct color from goods where name=?";
        colors = DaoUtil.queryList(String.class,sql,name);
        System.out.println(colors);
        return colors;
    }
}
